import MSData
import WorkerMSDB
import WorkerCOMSecurithor
import MSLogger
import heartbeating

heartbeat = heartbeating.HeartbeatListener()
heartbeat.start()

MSLogger.logInit('C:\\Soft\\ms_logs\\ms.service.log', 6, True)
MSLogger.logInfo('ms driver started')
reader = WorkerMSDB.Worker('localhost', '1433', 'base', 'mssa', 'msms')
reader.connect()

COMPort = 3
writer = WorkerCOMSecurithor.Worker(COMPort)
writer.connect()

objects = []
MSDBTemplates.GET_MSObjects_FROM_CarOnline(reader.connection.cursor(), objects)
ctr = 0
try:
    while True:
        if ctr == 6:
            MSDBTemplates.GET_MSObjects_FROM_CarOnline(reader.connection.cursor(), objects)
            ctr = 0
        reader.readEventsPGSM(objects)
        reader.readEventsContactId(objects)
        reader.readEventsBolid(objects)
        writer.writeEventsStatic(objects)
        reader.finalyze(objects)
        ctr += 1
        time.sleep(5)
 
except KeyboardInterrupt:
    MSLogger.logError('ms daemon stopped')
    heartbeat.stop()
except Exception, err:
    MSLogger.logError('ms driver fatal error: %s' % (str(err)))
    heartbeat.stop()
